﻿using ApiData.JsonDB;
using Newtonsoft.Json.Linq;
using SqlSugar;

namespace ApiData.Logic
{
    public class Like
    {
        public static Common.ResultData Execute(Model.tables tab, string? value, string? value2)
        {
            var path = tab.jsondata;
            var phy = Directory.GetCurrentDirectory();
            var real = phy + path;
            var cols = tab.structure.Split("\r\n");

            List<Model.Dto.Structure> st = new List<Model.Dto.Structure>();
            foreach (var item in cols)
            {
                if (item.IsNullOrWhiteSpace()) continue;
                var col = item.Split('-');
                st.Add(new Model.Dto.Structure()
                {
                    name = col[0],
                    type = col[1],
                    defaultvalue = col[2],
                    des = col[3]
                });
            }
            var db = new InitDB(real);
            var data = new object();
            int count = 0;

            var primary = st.FirstOrDefault(x => x.name == value);
            if (primary.IsNull()) return new Common.ResultData().Fail("没有需要查询的字段");
            data = db.WhereLike(primary.name, value2, out count);

            if (data == null)
                return new Common.ResultData().Fail("找不到相关信息");
            else
                return new Common.ResultData().SuccessData(count, data);
        }
    }
}